﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Crosland.Automotriz.Core;
using System.Data.SqlClient;
namespace Crosland.Automotriz.Data.Dao.Linq
{
    public class clsT_Man_SistemaDAO
    {

        private AutomotrizEntities ContextoDeObjs = new AutomotrizEntities();

        public IEnumerable<Object> listar_filtro_modelo_sistema(int id_modelo)
        {
            IEnumerable<Object> lista = new List<Object>();
            try
            {
                var q = from tmm in ContextoDeObjs.T_MAN_SISTEMA_MODELO
                        from tms in ContextoDeObjs.T_MAN_SISTEMA
                        from tm in ContextoDeObjs.TD_MODELO
                        from tma in ContextoDeObjs.T_MAN_SISTEMA_ARTICULO
                        where tm.ID_MODELO == tmm.ID_MODELO && tms.ID_SISTEMA == tmm.ID_SISTEMA
                        && tma.ID_SISTEMA == tms.ID_SISTEMA && tma.ID_MODELO == tmm.ID_MODELO
                        && tmm.ID_MODELO == id_modelo
                        group tmm by new
                        {
                            tma.NRO_PAGINA,
                            tma.ID_SISTEMA,
                            tmm.ID_MODELO,
                            tms.DES_SISTEMA,
                            tms.DES_SISTEMA_INGLES,
                            tmm.DES_IMAGEN
                        } into g
                        select new
                        {
                            NRO_PAGINA = g.Key.NRO_PAGINA,
                            ID_SISTEMA = g.Key.ID_SISTEMA,
                            ID_MODELO = g.Key.ID_MODELO,
                            DES_SISTEMA = g.Key.DES_SISTEMA,
                            DES_SISTEMA_INGLES = g.Key.DES_SISTEMA_INGLES,
                            DES_IMAGEN = g.Key.DES_IMAGEN
                        };

                lista = q.OrderBy(x => x.NRO_PAGINA).ToList();
            }
            catch (Exception)
            {
                throw;
            }
            return lista;

        }







        //augusto

        public List<T_MAN_SISTEMA> listarSistema()
        {
            List<T_MAN_SISTEMA> listSistema = new List<T_MAN_SISTEMA>();
            try
            {
                var q = from c in ContextoDeObjs.T_MAN_SISTEMA
                        where c.IND_ELIMINAR == 0
                        select c;
                listSistema = q.ToList();

            }
            catch (Exception)
            {

            }
            return listSistema;
        }



        public List<T_MAN_SISTEMA> listarSistema2()
        {
            List<T_MAN_SISTEMA> listSistema = new List<T_MAN_SISTEMA>();
            try
            {
                var q = from c in ContextoDeObjs.T_MAN_SISTEMA
                        
                        select c;
                listSistema = q.OrderBy(x=>x.ID_SISTEMA).ToList();
                

            }
            catch (Exception)
            {

            }
            return listSistema;
        }


        public List<T_MAN_SISTEMA> listarSistema_filtro(string descripcion)
        {
            List<T_MAN_SISTEMA> listSistema = new List<T_MAN_SISTEMA>();
            try
            {
                var q = from c in ContextoDeObjs.T_MAN_SISTEMA
                        where c.IND_ELIMINAR == 0 && c.DES_SISTEMA.Contains(descripcion) // || c.DES_DESCRIPCION_US.Contains(descripcion)
                        select c;
                listSistema = q.ToList();

            }
            catch (Exception)
            {

            }
            return listSistema;
        }





        public IEnumerable<Object> Listar()
        {
            IEnumerable<Object> lista = new List<Object>();
            try
            {
                var q = from s in ContextoDeObjs.T_MAN_SISTEMA
                        from t in ContextoDeObjs.T_MAN_SISTEMA_MODELO
                        from m in ContextoDeObjs.TD_MODELO
                        where s.ID_SISTEMA == t.ID_SISTEMA && t.ID_MODELO == m.ID_MODELO && s.IND_ELIMINAR == 0
                        select new
                        {
                            s.ID_SISTEMA,
                            s.DES_SISTEMA,
                            s.DES_SISTEMA_INGLES,
                            t.DES_IMAGEN

                        };
                lista = q.ToList();
            }
            catch (Exception)
            {
            }
            return lista;
        }



        public bool Insertar(T_MAN_SISTEMA Obj)
        {
            try
            {
                ContextoDeObjs.AddObject("T_MAN_SISTEMA", Obj);
                ContextoDeObjs.SaveChanges();
            }
            catch (Exception)
            {
                return false;
            }
            return true;
        }

        public bool Modificar(T_MAN_SISTEMA obj)
        {
            try
            {
                ContextoDeObjs.AttachTo("T_MAN_SISTEMA", obj);
                ContextoDeObjs.ObjectStateManager.ChangeObjectState(obj, System.Data.EntityState.Modified);
                ContextoDeObjs.SaveChanges();
            }
            catch (Exception)
            {
                return false;

            }
            return true;
        }

        public bool Eliminar(T_MAN_SISTEMA obj)
        {
            try
            {
                ContextoDeObjs.AttachTo("T_MAN_SISTEMA", obj);
                ContextoDeObjs.DeleteObject(obj);
                ContextoDeObjs.SaveChanges();
            }
            catch (Exception)
            {
                return false;
            }
            return true;
        }

        public int ObtenerIdSistema()
        {
            int id_opcion = 0;

            try
            {
                var q = (from c in ContextoDeObjs.T_MAN_SISTEMA.ToList()
                         select c.ID_SISTEMA).DefaultIfEmpty(0).Max();

                id_opcion = q;

                //id_opcion = lis[0].OPCION_ID;

            }
            catch (Exception)
            {
            }
            return id_opcion + 1;
        }

        public int obtenerId(string des_ingles)
        {
            List<T_MAN_SISTEMA> listaIdSistema = new List<T_MAN_SISTEMA>();
            int idSistema = 0;
            try
            {
                var q = from tmsa in ContextoDeObjs.T_MAN_SISTEMA
                        where tmsa.DES_SISTEMA_INGLES == des_ingles
                        select tmsa;
                idSistema = q.ToList()[0].ID_SISTEMA;

            }
            catch (Exception)
            {


            }

            return idSistema;
        }


    }
}
